package a66weding.com.jiehuntong.activity.home.hotel;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.home.hotel.hotelnew.HomeHotelActivity2;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * Created by admin on 2016/9/11.
 * 酒店预订套餐页面
 */
public class HomeTaocanFragment1 extends Fragment implements OnNetResultListener,AdapterView.OnItemClickListener{

    private HomeHotelActivity2 mHomeHotelActivity;
    private XRefreshView xRefreshView;
   private ListView list;
    private Hoteladapter1 hoteladapter1;
    private int categoryId;
    private boolean  isDownRefresh = true;
    //套餐的数据;
    private List<HomeBotomListBean.DataBean> hotelListData;
    //页数;
    //会员id
    private boolean islogin;
    private int memBerId;
    private int page;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(),R.layout.activity_hometaocanfragment1,null);
        initRefreshView(view);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        islogin = ((GlobalApplication) getActivity().getApplication()).isLogin();
        if (requestCode==Hoteladapter1.REQUSET_LOGIN_CODE&&resultCode== LoginActivity.LOGIN_OK){
            if(islogin) {
                memBerId = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
            }
            hoteladapter1.doCollect();
}
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHomeHotelActivity = (HomeHotelActivity2) getActivity();
        islogin = ((GlobalApplication) mHomeHotelActivity.getApplication()).isLogin();
        if(islogin) {
            memBerId = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        }
        categoryId =  ((HomeHotelActivity2)getActivity()).getCategoryId();
        page = 1;
        getdataFromNet(page);
    }

    private void initRefreshView(View view) {
        xRefreshView = (XRefreshView) view.findViewById(R.id.help_xrefreshview);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);

        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        list = (ListView) view.findViewById(R.id.hotel_list);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            list.setNestedScrollingEnabled(true);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            list.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if (list.getFirstVisiblePosition()>0){
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            list.setNestedScrollingEnabled(false);
                        }
                    }else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            list.setNestedScrollingEnabled(true);
                        }
                    }
                }
            });
        }


        list.setOnItemClickListener(this);
        hoteladapter1 = new Hoteladapter1(this);
        list.setAdapter( hoteladapter1);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isDownRefresh = true;
                page = 1;
                getdataFromNet(page);
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                ++page;
                getdataFromNet(page);
            }
        });
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        Object data = sucessbean.getData();
        if (data instanceof HomeBotomListBean){
            HomeBotomListBean looperData= (HomeBotomListBean) data;
            switch (looperData.getResCode()){
                case "0000":
                    hotelListData = looperData.getData();
                    if (isDownRefresh){
                        hoteladapter1.clear();
                    }
                    hoteladapter1.append(hotelListData);
                    break;
            }
        }
    }
    @Override
    public void onError(ErrorBean errorBean) {

    }
    private void getdataFromNet(int pageIndex){
        String url= RequestCode.get_ProductListByCategory;
        url += "&pageIndex="+pageIndex+"&category="+categoryId+"&memberID="+memBerId ;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,
                this, HomeBotomListBean.class,RequestCode.get_ProductListByCategory);
        HttpHandleUtil.executeCommonGet(getContext(),httpRequestBean);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //HomeBotomListBean.DataBean dataBean = (HomeBotomListBean.DataBean) hoteladapter1.getItem(position);
    }
}
